﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace MvcShopping.Models
{
    public class ProductCategoriesController : Controller
    {
        private MvcShoppingContext1 db = new MvcShoppingContext1();

        // GET: ProductCategories
        public ActionResult Index()
        {
            return View(db.ProdcutCategories.ToList());
        }

        // GET: ProductCategories/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProductCategory productCategory = db.ProdcutCategories.Find(id);
            if (productCategory == null)
            {
                return HttpNotFound();
            }
            return View(productCategory);
        }

        // GET: ProductCategories/Create
        public ActionResult Create()
        {
            return View(db.ProdcutCategories.ToList());
        }

        // POST: ProductCategories/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Createa(String name)
        {
            
            if (ModelState.IsValid)
            {
                ProductCategory productCategory = new ProductCategory();
                productCategory.Name = name;
                db.ProdcutCategories.Add(productCategory);
                db.SaveChanges();
                return RedirectToAction("Create");
            }

            return View("~Admin/Index");
        }

        // GET: ProductCategories/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProductCategory productCategory = db.ProdcutCategories.Find(id);
            if (productCategory == null)
            {
                return HttpNotFound();
            }
            return View(productCategory);
        }

        // POST: ProductCategories/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name")] ProductCategory productCategory)
        {
            if (ModelState.IsValid)
            {
                db.Entry(productCategory).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(productCategory);
        }

        // GET: ProductCategories/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProductCategory productCategory = db.ProdcutCategories.Find(id);
            if (productCategory == null)
            {
                return HttpNotFound();
            }
            return View(productCategory);
        }

        // POST: ProductCategories/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ProductCategory productCategory = db.ProdcutCategories.Find(id);
            db.ProdcutCategories.Remove(productCategory);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        ///删除
        public void deleteType(int id) {
            ProductCategory productCategory = db.ProdcutCategories.Find(id);
            db.ProdcutCategories.Remove(productCategory);
            db.SaveChanges();
        }

        //修改名字
        public ActionResult Updatea(String id, String name) {
            ProductCategory productCategory = new ProductCategory();
            productCategory.Name = name;
            productCategory.Id = int.Parse(id);
            db.Entry(productCategory).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Create");
        }
    }
}
